package com.szholly.data.general;

import com.szholly.utils.util.ObjectUtils;
import com.szholly.utils.util.StringUtils;

/**
 * 类功能说明 定义连接字符串解析类
 */
public class ConnectionInfo {

	/*
	 * 地图url地址(连接信息表中连接url为地图url时)
	 */
	private String serviceUrl;

	public ConnectionInfo(String connectionStr, String serviceUrl) {
		GetPropertySet(connectionStr);
		this.serviceUrl = serviceUrl;
	}

	/**
	 * @Title: GetPropertySet
	 * @Description: 解析数据库连接的字符串获得IPropertySet
	 * @param @param strCon
	 * @param @return
	 * @return String 返回类型
	 * @throws
	 */
	private void GetPropertySet(String strCon) {
		if (!StringUtils.IsNullOrSpace(strCon)) {
			String[] arrcon = strCon.split(";");
			for (String item : arrcon) {
				if (StringUtils.IsNullOrEmpty(item))
					continue;

				String[] parameters = item.split("=");
				// 转换成大写
				String tempStr = parameters[0].toUpperCase().trim();
				if ("DATABASE".equals(tempStr)) {
					if (parameters.length > 1)
						database = parameters[1];
				} else if ("NETNAME".equals(tempStr)) {
					if (parameters.length > 1)
						netName = parameters[1];
				} else if ("SERVER".equals(tempStr)) {
					if (parameters.length > 1)
						server = parameters[1];
				} else if ("USERID".equals(tempStr)) {
					if (parameters.length > 1)
						userID = parameters[1];
				} else if ("PASSWORD".equals(tempStr)) {
					if (parameters.length > 1)
						password = parameters[1];
				} else if ("PORT".equals(tempStr)) {
					if (parameters.length > 1)
						port = parameters[1];
				} else if ("VERSION".equals(tempStr)) {
					if (parameters.length > 1)
						version = parameters[1];
				}
			}
		}
	}

	/**
	 * 服务类型
	 */
	private UrlType urlType;

	public UrlType getUrlType() {
		return urlType;
	}

	public void setUrlType(UrlType value) {
		urlType = value;
	}
	
	/**
	 * 工作空间类型
	 */
	private WorkspaceType workspaceType;

	public WorkspaceType getWorkspaceType() {
		return workspaceType;
	}

	public void setWorkspaceType(WorkspaceType value) {
		workspaceType = value;
	}

	/**
	 * 数据库名称或数据库文件路径
	 */
	private String database = "";

	public final String getDatabase() {
		return database;
	}

	public final void setDatabase(String value) {
		database = value;
	}

	/**
	 * 网络名
	 */
	private String netName = "";

	public final String getNetName() {
		return netName;
	}

	public final void setNetName(String netName) {
		this.netName = netName;
	}

	private String server = "";

	/**
	 * 服务器名称
	 */
	public final String getServer() {
		return server;
	}

	public final void setServer(String server) {
		this.server = server;
	}

	private String userID = "";

	/**
	 * 用户名
	 */
	public final String getUserID() {
		return userID;
	}

	public final void setUserID(String userID) {
		this.userID = userID;
	}

	private String password = "";

	/**
	 * 密码
	 */
	public final String getPassword() {
		return password;
	}

	public final void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 网络端口号
	 */
	private String port = "";

	public final String getPort() {
		return port;
	}

	public final void setPort(String port) {
		this.port = port;
	}

	private String version = "";

	/**
	 * 数据版本
	 */
	public final String getVersion() {
		return version;
	}

	public final void setVersion(String version) {
		this.version = version;
	}

	/**
	 * 获取连接字符串
	 */
	public String getConnectionStr() {
		StringBuilder sb = new StringBuilder();
		if (StringUtils.IsNullOrSpace(database))
			sb.append("DATABASE=;");
		else
			sb.append("DATABASE=" + database.toUpperCase().trim() + ";");

		if (StringUtils.IsNullOrSpace(netName))
			sb.append("NETNAME=;");
		else
			sb.append("NETNAME=" + netName.toUpperCase().trim() + ";");

		if (StringUtils.IsNullOrSpace(server))
			sb.append("SERVER=;");
		else
			sb.append("SERVER=" + server.toUpperCase().trim() + ";");

		if (StringUtils.IsNullOrSpace(userID))
			sb.append("USERID=;");
		else
			sb.append("USERID=" + userID + ";");

		if (StringUtils.IsNullOrSpace(password))
			sb.append("PASSWORD=;");
		else
			sb.append("PASSWORD=" + password + ";");

		if (StringUtils.IsNullOrSpace(port))
			sb.append("PORT=;");
		else
			sb.append("PORT=" + port + ";");

		if (StringUtils.IsNullOrSpace(version))
			sb.append("VERSION=");
		else
			sb.append("VERSION=" + version);

		return sb.toString();
	}

	/*
	 * 获取服务url地址
	 */
	public String getServiceUrl() {
		return this.serviceUrl;
	}

	public Boolean Equals(Object obj) {
		ConnectionInfo info = (ConnectionInfo) obj;
		if (info == null)
			return false;

		if (ObjectUtils.ObjectEquals(workspaceType, info.getWorkspaceType())
				&& ObjectUtils.ObjectEquals(this.getConnectionStr(),
						info.getConnectionStr()))
			return true;
		else
			return false;
	}

	public int GetHashCode() {
		String msg = "" + workspaceType.hashCode();
		msg += this.getConnectionStr();
		return msg.hashCode();
	}
}
